package com.dayang.homemaking.common.core.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlInjectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dayang.homemaking.common.core.domain.PageQuery;
import com.dayang.homemaking.common.core.domain.PageResult;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author: dayang
 * @since: 2024/9/14
 * @description: mp分页工具类
 */
@Slf4j
public class MpPageUtil {
    private MpPageUtil(){}

    /**
     * 转换为mp分页对象
     * @param pageQuery 分页参数
     */
    public static Page<?> convert2PageQuery(PageQuery pageQuery){
        Page<?> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        List<PageQuery.SortItem> sortItemList = pageQuery.getSortItemList();
        if (CollUtil.isEmpty(sortItemList)){
            return page;
        }
        // 设置排序字段并检测是否有sql注入
        List<OrderItem> orderItems = new ArrayList<>(sortItemList.size());
        for (PageQuery.SortItem sortItem : sortItemList) {
            if (StrUtil.isBlank(sortItem.getColumn())){
                continue;
            }
            if (SqlInjectionUtils.check(sortItem.getColumn())){
                log.error("字段【{}】存在SQL注入风险",sortItem.getColumn());
                throw new UnsupportedOperationException("存在SQL注入风险，请联系技术工作人员！");
            }
            OrderItem orderItem = new OrderItem();
            orderItem.setColumn(sortItem.getColumn());
            orderItem.setAsc(sortItem.getIsAsc());
            orderItems.add(orderItem);
        }
        page.setOrders(orderItems);
        return page;
    }

    /**
     * 转换为分页结果对象
     */
    public static <T> PageResult<T> convert2PageResult(Page<T> page) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setTotal(pageResult.getTotal());
        pageResult.setPages(pageResult.getPages());
        pageResult.setList(page.getRecords());
        return pageResult;
    }

    /**
     * 转换为分页结果对象(源对象和目标对象属性名称相同)
     */
    public static <T, E> PageResult<T> convert2PageResult(Page<?> page, List<E> sourceList, Class<T> targetClazz) {
        return convert2PageResult(page, BeanUtil.copyToList(sourceList, targetClazz));
    }

    /**
     * 转换为分页结果对象(源对象和目标对象属性名称不相同，自定属性映射逻辑)
     */
    public static <R,T> PageResult<T> convert2PageResult(Page<R> page, Function<R, T> convertor){
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setTotal(pageResult.getTotal());
        return convert2PageResult(page,page.getRecords().stream().map(convertor).collect(Collectors.toList()));
    }

    public static <T> PageResult<T> convert2PageResult(Page<?> page, List<T> sourceList) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setTotal(pageResult.getTotal());
        pageResult.setPages(pageResult.getPages());
        pageResult.setList(sourceList);
        return pageResult;
    }
}
